//
//  ViewController.m
//  04-UIControl
//
//  Created by QC.L on 16/7/19.
//  Copyright © 2016年 QC.L. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor blackColor];
    // 一、UISwitch(开关)
    // Switch 宽和高不影响控件的大小, 只有x和y
    UISwitch *switchControl = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
    [self.view addSubview:switchControl];
    // 1.开启时的控件内部颜色
    switchControl.onTintColor = [UIColor yellowColor];
    // 2.边框及切换到关闭时的颜色 (null_resettable)
    switchControl.tintColor = nil;
    // 3.开关按钮(圆圈)的颜色
    switchControl.thumbTintColor = [UIColor blackColor];
    // 4.打开时的图片, iOS7后失效
    switchControl.onImage = [UIImage imageNamed:@"121"];
    // 5.关闭时的图片, iOS7后失效
    switchControl.offImage = [UIImage imageNamed:@"121"];
    // 6.判断switchControl的开闭状态 NO 为 关闭状态 YES 为 开启状态
    // 通过setter方法, 改变switchControl的初始状态
    switchControl.on = YES;
    NSLog(@"%@", switchControl.isOn ? @"YES" : @"NO");
    [switchControl setOn:YES animated:YES];
    // 7.添加绑定事件(与Button相同)
    [switchControl addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    
    // 二、UISlider
    // 高度不影响视图的高度, 这个控件用于居中于视图中间
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 200, 355, 50)];
    [self.view addSubview:slider];
    // 1.当前滑块的值
    slider.value = 0.5f;
    // 2.滑块最大值 1.0
    slider.maximumValue = 1.0f;
    // 3.滑块最小值 0.0
    slider.minimumValue = 0.0f;
    // 以上最大和最小可以根据需求去改变
    // 4.minimumValueImage 最小值那边显示的图片
    // 5.maximumValueImage 最大值那边显示的图片
    // 6.continuous
    slider.continuous = NO;
    // 7.绑定事件
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    // 8.maximumTrackTintColor 未移动到的区域颜色
//    slider.maximumTrackTintColor = [UIColor yellowColor];
    // 9.minimumTrackTintColor 已移动到的区域颜色
//    slider.minimumTrackTintColor = [UIColor blueColor];
    // 10.滑块的颜色
//    slider.thumbTintColor = [UIColor blackColor];
    // 11.设置进度条滑块右边的图片
    [slider setMaximumTrackImage:[UIImage imageNamed:@"player_slider_playback_right"] forState:UIControlStateNormal];
    // 12.设置进度条滑块左边的图片
    [slider setMinimumTrackImage:[UIImage imageNamed:@"player_slider_playback_left"] forState:UIControlStateNormal];
    // 13.设置进度条滑块图片
    [slider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"] forState:UIControlStateNormal];
    
    // UISegmentedControl
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"first", @"lastlastlastlastlastlastlast"]];
    segment.tag = 1000;
    // segment不给frame, 系统会自动计算大小(推荐: 加上frame)
    NSLog(@"%@", NSStringFromCGRect(segment.frame));
    segment.frame = CGRectMake(20, 20, 335, 50);
    // 1.momentary 设置为YES, 如果点击选中没有选中的效果
    // 默认为NO
    segment.momentary = YES;
    // 2.numberOfSegments
    NSUInteger numberOfSegments = segment.numberOfSegments;
    NSLog(@"分段控制器的Item的个数: %lu", numberOfSegments);
    [self.view addSubview:segment];
    // 练习:
    // 点击Button, 使Segment添加新的标题
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(20, 200, 300, 40);
    button.backgroundColor = [UIColor brownColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(addSegmentTitle:) forControlEvents:UIControlEventTouchUpInside];
    // 3.insertSegmentWithTitle:atIndex:animated:
    // 4.remove
    // 5.setTitle:forSegmentAtIndex: 用来修改对应下标的标题
//    [segment setTitle:@"HuangXiao" forSegmentAtIndex:0];
    // 6.setWidth:forSegmentAtIndex: width默认为0.0
    [segment setWidth:0.0 forSegmentAtIndex:0];
    // 7.setContentOffset:forSegmentAtIndex:
    // 设置对应下标内容的偏移
    [segment setContentOffset:CGSizeMake(0, 0) forSegmentAtIndex:0];
    // 8.enabled 是否能被选中
    [segment setEnabled:YES forSegmentAtIndex:0];
    // 9.selectedSegmentIndex 选中的下标
    segment.selectedSegmentIndex = 1;
    // 10.tintColor文字及边框颜色
    segment.tintColor = [UIColor whiteColor];
    // 11.apportionsSegmentWidthsByContent 默认为NO
    // 如果分段控制器的段宽为0时, 并且该属性为YES, 系统会自动计算
    segment.apportionsSegmentWidthsByContent = YES;
    // 12.segment绑定事件
    [segment addTarget:self action:@selector(segmentedControlIndexChange:) forControlEvents:UIControlEventValueChanged];
    
    //
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(20, 400, 200, 50)];
    // 1.分页控制器的页数 总页数 默认为 0
    pageControl.numberOfPages = 5;
    // 2.当前页
    pageControl.currentPage = 0;
    // 3.当只有一页时隐藏
    pageControl.hidesForSinglePage = YES;
    // 4.获取到点的大小
    CGSize size = [pageControl sizeForNumberOfPages:1];
    NSLog(@"%@", NSStringFromCGSize(size));
    // 5.未选中页的默认颜色
    pageControl.pageIndicatorTintColor = [UIColor greenColor];
    // 6.当前页的颜色
    pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    [self.view addSubview:pageControl];
    // 7.绑定事件
    [pageControl addTarget:self action:@selector(pageControlChange:) forControlEvents:UIControlEventValueChanged];
    // 8.设置为YES, 关闭系统事件page的切换
    // 直到调用updateCurrentPageDisplay的方法才会显示
    pageControl.defersCurrentPageDisplay = YES;
    
    
}
// switch触发的事件
- (void)switchAction:(UISwitch *)switchControl {
    // 根据switch的状态, 决定触发什么样的事件
    if (switchControl.on == YES) {
        NSLog(@"开");
    } else {
        NSLog(@"关");
    }
}

- (void)sliderAction:(UISlider *)slider {
    NSLog(@"%f", slider.value);
}

- (void)addSegmentTitle:(UIButton *)button {
    UISegmentedControl *segment = [self.view viewWithTag:1000];
    [segment insertSegmentWithTitle:@"Huang" atIndex:0 animated:YES];
}

- (void)segmentedControlIndexChange:(UISegmentedControl *)segmentedControl {
    switch (segmentedControl.selectedSegmentIndex) {
        case 1:
            break;
        case 2:
            break;
        default:
            break;
    }
}

- (void)pageControlChange:(UIPageControl *)pageControl {
    NSLog(@"Change");
    [pageControl updateCurrentPageDisplay];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
